/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
var isEdit = false;
var parent;

zm.ready(function() {




    // select combobox event
    zm("#choice-answer").change(function() {
        var type = zm(this).val();
        if (type === "0") {
            zm(".content-answer").html(getText());
        }
        if (type === "1") {
            zm(".content-answer").html(getParagraph());
        }
        if (type === "2" || type === "3") {
            zm(".content-answer").html(getMultiChoice());
            // and event for button add
            zm(".add-option").bind("click", function() {
                zm(".add-option").before(getChoice());
                //removeButton();
                zm(".remove").unbindAll();
                zm(".remove").bind("click", function() {
                    zm(this).parent().remove();
                    //removeButton();
                });
            });
//                         //and event for remove button
//                         zm(".remove").bind("click",function (){
//                             zm(this).parent().remove();
//                         });

        }
    });

    ///////

    // done question
    zm(".done-question").bind("click", function() {
      if(!isEdit){
            zm(".form-question").append(addquestion());
            isEdit = false;
      }
       else{
            parent.html(addquestion());
            isEdit =false;
       }
   
   
      
        zm(".question").hide();

        // raise delete question event
        zm('.remove-answer').unbindAll();
        zm('.remove-answer').bind("click", function() {
            zm(this).parent().parent().remove();
        });

        // raise edit question event
        zm('.edit-answer').bind("click", function() {
           isEdit = true;
           parent = zm(this).parent().parent();
           editquestion();
        });

    });
    
    
    
    
    zm(".add-item").click(function() {
        zm(".question").find("input").each(function() {
            zm(this).val("");
        });
        zm(".question").show();
    });

});



function addquestion(){
        zm(".form-question").addClass("boder-style");
        var require = zm('input[name="require"]').attr("checked");
        var title = zm('input[name="title"]').attr("value");
        var help = zm('input[name="help-text"]').attr("value");
        var type = zm('#choice-answer').val();
        var question = '<div class="question-answer" >';

        question += '<div class="edit non-display"><button class="remove-answer " >X</button>';
        question += '<button class="edit-answer" >Edit</button></div><p class="question-title">' + title;
        if (require === true) {
            question += '<span  class="require" requirement="1">(*)</span></p>';
        }
        else {
            question += '<span class="require" requirement="0"></span></p>';
        }

        question += '<p class="question-helptext">' + help + '</p>';
        if (type === "0") {
            question += '<div class="answer" typeanswer="0">';
            question += '<input type="text" size="100"/>';
        }
        if (type === "1") {
            question += '<div class="answer" typeanswer="1">';
            question += '<textarea cols="50" ></textarea>';

        }
        if (type === "3") {
            question += '<div class="answer" typeanswer="3">';
            zm(".option").each(function() {
                var txt = zm(this).find('input[type="text"]').val();
                question += '  <div class="answer-option">'
                        + '<input type="checkbox" ><span>' + txt + '</span>'
                        + '</div>';
            });
        }
        if (type === "2") {
            question += '<div class="answer" typeanswer="2">';
            var name = getNameforRadiobox();
            zm(".option").each(function() {
                var txt = zm(this).find('input[type="text"]').val();
                question += '<div class="answer-option" >'
                        + '<input type="radio" name="' + name.toString() + '"><span>' + txt + '</span>'
                        + '</div>';
            });

        }
        question += '</div>';
        question += '</div>';
        return question;
       
}

function showeditquestion(title, help, type, require, value) {
    zm('input[name="require"]').attr("checked", require);
    zm('input[name="title"]').text(title);
    zm('input[name="help-text"]').text(help);
    var temp = "";
    zm('#choice-answer').val(type);
    for (var i = 0; i < value.length; i++) {
        temp += '<div class="option"><input type="radio" name="0" disabled=""><input type="text" size="50" value="' + value[i] + '"/><button class="remove">X</button><br></div>';
    }
    if(type==="2" || type==="3"){
        temp += '<button class="add-option">Add Option</button>';
    }
    
    zm(".content-answer").html(temp);
    // and event for button add
    zm(".add-option").bind("click", function() {
        zm(".add-option").before(getChoice());
        //removeButton();
        zm(".remove").unbindAll();
        zm(".remove").bind("click", function() {
            zm(this).parent().remove();
            //removeButton();
        });

    });

    zm(".remove").unbindAll();
    zm(".remove").bind("click", function() {
        zm(this).parent().remove();
        //removeButton();
    });

    zm(".question").show();
}

function editquestion() {
    //alert(parent.attr("questionid"));
    var title = parent.find(".question-title").text();
    var help = parent.find(".question-helptext").text();
    var type = parent.find(".answer").attr("typeanswer");
    var require = parent.find(".require").attr("requirement");
    var value = new Array();
    var i = 0;
    parent.find(".answer-option").each(function() {
        var temp = zm(this).find("span").text();
        value[i++] = temp;
    });
  
    showeditquestion(title, help, type, require, value);
}



//            
//            function read(str){
//                zm.get(str,success);
//            }
//            function success(data){
//                 zm(".type-answer").html(data);
//            }
//            function getAnswerAndBuild(){
//                
//            }
function removeButton() {
    var size = zm(".content-answer").children().size();
    if (size === 2) {
        zm(".remove").addClass("hide");
    }
    else {
        zm(".remove").removeClass("hide");
    }
}
function getNameforRadiobox() {
    var i = 0;
    var exits = true;
    do {
        i++;
        exits = false;
        zm('.answer-option input[type="radio"]').each(function() {
            var name = zm(this).attr("name");
            console.log("name" + name);
            if (name !== "" && i === zm.intval(name)) {
                exits = true;
            }
        });
    } while (exits === true);
    console.log("i:" + i);
    return i;
}